package com.yuexin.project.order.service;

import com.google.common.collect.Lists;
import com.yuexin.common.support.Convert;
import com.yuexin.project.order.domain.*;
import com.yuexin.project.order.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 报餐设置 服务层实现
 *
 * @author yuexin
 * @date 2018-10-22
 */
@Service
public class SettingServiceImpl implements ISettingService {
    @Autowired
    private MealsSettingMapper mealsSettingMapper;

    @Autowired
    private GuestUserMapper guestUserMapper;

    @Autowired
    private PeriUserMapper periUserMapper;

    @Autowired
    private ManagerUserMapper managerUserMapper;
    @Autowired
    private CookUserMapper cookUserMapper;

    /**
     * 查询报餐设置信息
     *
     * @param companyId 报餐设置ID
     * @return 报餐设置信息
     */
    @Override
    public MealsSetting selectSettingByCompanyId(String companyId) {
        return mealsSettingMapper.selectSettingByCompanyId(companyId);
    }

    /**
     * 查询报餐设置列表
     *
     * @param mealsSetting 报餐设置信息
     * @return 报餐设置集合
     */
    @Override
    public List<MealsSetting> selectSettingList(MealsSetting mealsSetting) {
        return mealsSettingMapper.selectSettingList(mealsSetting);
    }

    /**
     * 新增报餐设置
     *
     * @param mealsSetting 报餐设置信息
     * @return 结果
     */
    @Override
    public int insertSetting(MealsSetting mealsSetting) {
        if ("2".equals(mealsSetting.getGuestSetting())) {
            insertGuestUser(mealsSetting);
        }

        if ("2".equals(mealsSetting.getPeriSetting())) {
            insertPeriUser(mealsSetting);
        }

        return mealsSettingMapper.insertSetting(mealsSetting);
    }

    public void insertGuestUser(MealsSetting setting) {
        List<GuestUser> list = Lists.newArrayList();
        Integer[] userIds = setting.getManagerUserIds();
        if(userIds == null || userIds.length == 0){
            return;
        }
        for (Integer userId : userIds) {
            GuestUser gu = new GuestUser();
            gu.setGuestId(setting.getMealsId());
            gu.setUserId(userId);
            list.add(gu);
        }
        guestUserMapper.batchGuestUser(list);
    }

    public void insertPeriUser(MealsSetting setting) {
        List<PeriUser> list = new ArrayList<PeriUser>();
        Integer[] userIds = setting.getPeriUserIds();
        if(userIds == null || userIds.length == 0){
            return;
        }
        for (Integer userId : userIds) {
            PeriUser gu = new PeriUser();
            //gu.setGuestId(guestId);
            //gu.setGuestId(setting);
            gu.setPeriId(setting.getMealsId());
            gu.setUserId(userId);
            list.add(gu);
        }
        periUserMapper.batchPeriUser(list);
    }

    public void insertManagerUser(MealsSetting setting) {
        List<ManagerUser> list = Lists.newArrayList();
        Integer[] userIds = setting.getManagerUserIds();
        if(userIds == null || userIds.length == 0){
            return;
        }
        for (Integer userId : userIds) {
            ManagerUser gu = new ManagerUser();
            gu.setMealsId(setting.getMealsId());
            gu.setUserId(userId);
            list.add(gu);
        }
        managerUserMapper.batchManagerUser(list);
    }

    public void insertCookUser(MealsSetting setting) {
        List<CookUser> list = Lists.newArrayList();
        Integer[] userIds = setting.getCookUserIds();
        if(userIds == null || userIds.length == 0){
            return;
        }
        for (Integer userId : userIds) {
            CookUser cu = new CookUser();
            cu.setMealsId(setting.getMealsId());
            cu.setUserId(userId);
            list.add(cu);
        }
        cookUserMapper.batchCookUser(list);
    }

	/*private MealsSetting getRecoveryBean(){
		MealsSetting mealsSetting = new MealsSetting();
		mealsSetting.setMealsDate("1");
		mealsSetting.setMealsTime(2);
		mealsSetting.setIsBreakfast("0");
		mealsSetting.setBreakfastStart("06:00");
		mealsSetting.setBreakfastEnd("09:00");
		mealsSetting.setIsLunch("0");
		mealsSetting.setLunchStart("11:00");
		mealsSetting.setLunchEnd("14:00");
		mealsSetting.setIsDinner("0");
		mealsSetting.setDinnerStart("17:00");
		mealsSetting.setDinnerEnd("20:00");
		mealsSetting.setIsDishPic("0");
		mealsSetting.setIsSign("0");
		mealsSetting.setQrcodePassword("123456");
		mealsSetting.setGuestSetting("1");
		mealsSetting.setGuestDate("1");
		mealsSetting.setGuestTime(5);
		mealsSetting.setPeriSetting("1");
		mealsSetting.setPeriDate("1");
		mealsSetting.setPeriTime(5);
		return mealsSetting;
	}*/

    /**
     * 修改报餐设置
     *
     * @param mealsSetting 报餐设置信息
     * @return 结果
     */
    @Override
    public int updateSetting(Integer settingType, MealsSetting mealsSetting) {
        Integer mealsId = mealsSetting.getMealsId();
        //System.out.println("mealsId============================="+mealsId);
        // 删除客餐和用户关联
        guestUserMapper.deleteGuestUserByGuestId(mealsId);
        // 新增客餐和用户关联
        if ("2".equals(mealsSetting.getGuestSetting())) {
            insertGuestUser(mealsSetting);
        }

        // 删除围餐和用户关联
        periUserMapper.deleteSettingPeriUserById(mealsId);
        // 新增围餐和用户关联
        if ("2".equals(mealsSetting.getPeriSetting())) {
            insertPeriUser(mealsSetting);
        }

        managerUserMapper.deleteManagerUserById(mealsId);
        insertManagerUser(mealsSetting);

        cookUserMapper.deleteCookUserById(mealsId);
        insertCookUser(mealsSetting);

        //执行恢复
        if (settingType == 1) {
            //mealsSetting = getRecoveryBean();
            //mealsSetting.setMealsId(1);
            String companyId = mealsSetting.getCompanyId();
            mealsSetting = mealsSettingMapper.selectSettingByCompanyId("dingcan");
            mealsSetting.setMealsId(mealsId);
            mealsSetting.setCompanyId(companyId);
        }

        return mealsSettingMapper.updateSetting(mealsSetting);
    }

    /**
     * 删除报餐设置对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSettingByIds(String ids) {
        return mealsSettingMapper.deleteSettingByIds(Convert.toStrArray(ids));
    }

}
